<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<style>
    
    .box{
        width: 100px;
        height: 100px;
        background-color: skyblue;
        position: fixed;
        left: 0;
        top: 0;
    }

</style>
<body>
    <div class="box"></div>
    <script type="module">
        import Myevent from './myevent.js';

        let myevent = new Myevent();
        let count = 1;

        function move(divs , target,dir){
            var timer = setInterval(()=>{
            let num = parseInt(getComputedStyle(divs,null)[dir]); // 获取非行内样式
            // currentStyle 兼容性IE浏览器
            let speed = 15 *(target > num ? 1 : -1);
            if(num === target){
                clearInterval(timer);
                myevent.trigger('myevent'+count);
                count++;
            }else{
                divs.style[dir] = num + speed +'px';
            }
         },100)
        }

        var divs = document.querySelector('.box');

       
            move(divs,300,'left');
        
        myevent.addEvent('myevent1',function(){
            console.log(111);
            move(divs,300,'top')
        })

        myevent.addEvent('myevent2',function(){
            console.log(222);
            move(divs,0,'left')
        })

        myevent.addEvent('myevent3',function(){
            console.log(333);
            move(divs,0,'top')
        })
        myevent.addEvent('myevent4',function(){
            console.log('wangcl');
        })







        // 回调地狱
        // move(divs,300,'left',function(){
        //     move(divs,300,'top',function(){
        //         move(divs,0,'left',function(){
        //             move(divs,0,'top',function(){
        //                 console.log('执行完毕');
        //             })
        //         })
        //     })
        // })








    </script>
</body>
</html>